/**
 * 地址初初化及选择
 */
function setAddr() {
	var commonArea = areaAllJson, province_arr = [], city_arr = [], district_arr = [], provice_str = "<option>请选择</option>", city_str = "<option>请选择</option>", district_str = "<option>请选择</option>";
	// console.log(commonArea)
	for (var i = 0, length = commonArea.length; i < length; i++) {

		if (commonArea[i].parentId == 0) {
			var pro_obj = {
				areaCode : commonArea[i].areaCode,
				areaName : commonArea[i].areaName
			};
			province_arr.push(pro_obj);
			if (provinceId == commonArea[i].areaCode) {
				provice_str += '<option selected="selected" _value="'
						+ commonArea[i].areaCode + '" value="'
						+ commonArea[i].areaName + '">'
						+ commonArea[i].areaName + '</option>';
			} else {
				provice_str += '<option _value="' + commonArea[i].areaCode
						+ '" value="' + commonArea[i].areaName + '">'
						+ commonArea[i].areaName + '</option>';
			}
		}
		if (null != provinceId && provinceId.length != 0) {
			if (commonArea[i].parentId == provinceId) {
				var pro_obj = {
					areaCode : commonArea[i].areaCode,
					areaName : commonArea[i].areaName
				};
				city_arr.push(pro_obj);
				if (null != cityId && cityId.length != 0) {
					if (cityId == commonArea[i].areaCode) {
						city_str += '<option selected="selected" _value="'
								+ commonArea[i].areaCode + '" value="'
								+ commonArea[i].areaName + '">'
								+ commonArea[i].areaName + '</option>';
					} else {
						city_str += '<option _value="' + commonArea[i].areaCode
								+ '" value="' + commonArea[i].areaName + '">'
								+ commonArea[i].areaName + '</option>';
					}
				}
			}
			if (null != cityId && cityId.length != 0) {
				if (commonArea[i].parentId == cityId) {
					var pro_obj = {
						areaCode : commonArea[i].areaCode,
						areaName : commonArea[i].areaName
					};
					district_arr.push(pro_obj);
					if (null != districtId && districtId.length != 0) {
						if (districtId == commonArea[i].areaCode) {
							district_str += '<option selected="selected" _value="'
									+ commonArea[i].areaCode
									+ '" value="'
									+ commonArea[i].areaName
									+ '">'
									+ commonArea[i].areaName + '</option>';
						} else {
							district_str += '<option _value="'
									+ commonArea[i].areaCode + '" value="'
									+ commonArea[i].areaName + '">'
									+ commonArea[i].areaName + '</option>';
						}
					}
				}
			}
		}
	}
	$("#district").html(district_str);
	$("#city").html(city_str);
	$("#province").html(provice_str);

	$("#district").selects({
		id : "ubasic_county",
		cn : "ubasic",
		minWidth : 143,
		maxWidth : 198,
		callBack : function(val) {
			if($("#district option:selected").attr("_value")==null) {
				$("#districttext").html("");
				$("#area_id").val("");
				return;
			}
			$("#districttext").html(val);
			$("#area_id").val($("#district option:selected").attr("_value"));
		}
	});

	$("#city")
			.selects(
					{
						id : "ubasic_city",
						cn : "ubasic",
						minWidth : 143,
						maxWidth : 198,
						callBack : function(val) {
							if($("#city option:selected").attr("_value")==null) {
								$("#district option:first").attr("selected",true);
								$("#district").selectSync();
								
								$("#citytext").html("");
								$("#districttext").html("");
								return;
							}
							var cityCode = $("#city option:selected").attr("_value");
							$("#citytext").html(val);
							for (var i = 0, length = commonArea.length; i < length; i++) {
								var has_city = false;
								if (cityCode == commonArea[i].areaCode) {
									var c_str = "<option>请选择</option>";
									for (var j = 0, length2 = commonArea.length; j < length2; j++) {
										if (commonArea[j].parentId == commonArea[i].areaCode) {
											has_city = true;
											c_str += '<option _value="'
													+ commonArea[j].areaCode
													+ '" ' + 'value="'
													+ commonArea[j].areaName
													+ '">'
													+ commonArea[j].areaName
													+ '</option>';
										}
									}
									if (has_city) {
										$("#district").next().show();
										$("#district").html(c_str);
										$("#district").selectSync();
									} else {
										$("#district").next().hide();
									}
								}
							}
						}
					});

	$("#province")
			.selects(
					{
						id : "ubasic_province",
						cn : "ubasic",
						minWidth : 143,
						maxWidth : 198,
						callBack : function(val) {
							if($("#province option:selected").attr("_value")==null) {
								$("#city option:first").attr("selected",true);
								$("#district option:first").attr("selected",true);
								$("#city").selectSync();
								$("#district").selectSync();
								
								$("#provincetext").html("");
								$("#citytext").html("");
								$("#districttext").html("");
								return;
							}
							var privinceCode = $("#province option:selected").attr("_value");
							$("#provincetext").html(val);
							for (var i = 0, length = province_arr.length; i < length; i++) {
								if (privinceCode == province_arr[i].areaCode) {
									var c_str = "<option>请选择</option>";
									for (var j = 0, length2 = commonArea.length; j < length2; j++) {
										if (commonArea[j].parentId == province_arr[i].areaCode) {
											c_str += '<option _value="'
													+ commonArea[j].areaCode
													+ '" ' + 'value="'
													+ commonArea[j].areaName
													+ '">'
													+ commonArea[j].areaName
													+ '</option>';
										}
									}
									$("#city").html(c_str);
									$("#city").selectSync();
								}
							}
						}
					});

}

/**
 * 校验input工具类
 * obj需要校验的input对象
 * commitBtn触发校验按钮
 * callback 校验成功以后回调函数
 * tips  校验不成功提示信息
 * sourceValue  初始提示信息
 */
function notEmptyValidate(obj, commitBtn, callback, tips, sourceValue) {
	var _dom = $(obj).data("prefix",tips).data("conditions", [7]).data("placeholder",sourceValue);
	executeValidate(_dom, commitBtn, callback);
}

function onlyNumber(obj, commitBtn, callback, tips, sourceValue) {
	var _dom = $(obj).data("prefix",tips).data("conditions", [12]).data("placeholder",sourceValue);
	executeValidate(_dom, commitBtn, callback);
}

function validatePhone(obj, commitBtn, callback, tips, sourceValue) {
	var _dom = $(obj).data("prefix",tips).data("conditions", [15]).data("placeholder",sourceValue);
	executeValidate(_dom, commitBtn, callback);
}


function validateEmail(obj, commitBtn, callback, tips, sourceValue) {
	var _dom = $(obj).data("prefix",tips).data("conditions", [3]).data("placeholder",sourceValue);
	executeValidate(_dom, commitBtn, callback);
}

function validatePostCode(obj, commitBtn, callback, tips, sourceValue) {
	var _dom = $(obj).data("prefix",tips).data("conditions", [17]).data("placeholder",sourceValue);
	executeValidate(_dom, commitBtn, callback);
}

function validatePassword(obj, commitBtn, callback, tips, sourceValue) {
	var _dom = $(obj).data("prefix",tips).data("conditions", [1]).data("placeholder",sourceValue);
	executeValidate(_dom, commitBtn, callback);
}

function validateUserName(obj, commitBtn, callback, tips, sourceValue) {
	var _dom = $(obj).data("prefix",tips).data("conditions", [7,8]).data("placeholder",sourceValue);
	executeValidate(_dom, commitBtn, callback);
}

function executeValidate(_dom, commitBtn, callback) {
	ehaoyao.plugin.validation({
		dom:_dom,
		escaped:true,
		tips:function(msg, rel, judge){
			var _t=this;
			if(rel){
				_t=rel;
			}
			_alert(_t.data("prefix"));
		},
		sub_btn:commitBtn,
		sub_fn:function(){
			callback();
		}
	});
}


var inter = null;
var time = 0;
/**
 * 闪烁组件
 * @param obj
 */
function twinkle(obj, twtime) {
	if(!twtime) twtime = 4;
	inter = setInterval(function(){
		time++;
		obj.fadeOut(100).fadeIn(100);
		if(time == twtime) {
			time = 0;
			clearInterval(inter);
		}},200);
}

/**
 * 必须传年月日
 */
function getAges(str) {
	var r = str.split('-');     
    if(r==null || r.length < 3) return -1;     
    var d = new Date(r[0], r[1], r[2]);
    var today = new Date();
    var age = today.getFullYear() - parseInt(r[0]);
    if(today.getMonth()+1 < parseInt(r[1])) {
    	return age - 1;
    } else if(today.getMonth()+1 > parseInt(r[1])) {
    	return age;
    } else if(today.getDate() >= parseInt(r[2])){
    	return age;
    } else {
    	return age -1;
    }
    
    return -1;   
}
